<?php

$config["dbsetup"][]="xAPProcessTelephoneDatabase";

$config["xAPInc"][]=array("class"=>"cid.incoming","function"=>"xAPProcessCidIncoming"); // register receiver
$config["xAPInc"][]=array("class"=>"cti.info","function"=>"xAPProcessCTILine"); // register receiver
$config["xAPInc"][]=array("class"=>"cti.event","function"=>"xAPProcessCTILine"); // register receiver
$config["xAPInc"][]=array("class"=>"messender.event","function"=>"xAPProcessMessenger"); // register receiver
$config["xAPInc"][]=array("class"=>"messender.info","function"=>"xAPProcessMessenger"); // register receiver
$config["xAPInc"][]=array("class"=>"cid.meteor","function"=>"xAPProcessCidMeteor"); // register receiver

function xAPProcessMessenger($msg){

	$sql="insert into xAP_Messenger 
	(MessengerID,Network,xAPSource,Username,FullName,State,DateStamp) VALUES
	(Null,
	'".strtolower(dbesc($msg["body"]["user.status"]["network"]))."',
	'".strtolower(dbesc($msg["header"]["source"]))."',
	'".strtolower(dbesc($msg["body"]["user.status"]["username"]))."',
	'".(dbesc($msg["body"]["user.status"]["name"]))."',
	'".(dbesc($msg["body"]["user.status"]["status"]))."',
	'".dbesc(date("Y-m-d H:i:s"))."')";
	dbexec($sql);
	xAPLog("xAP","IM Updated ".$msg["header"]["source"],"");
}

function xAPProcessCTILine($msg){
	$sql="select LineID from xAP_Telephone_Line where LinexAPSource='".strtolower(dbesc($msg["header"]["source"]))."'";
	$results=dbquery($sql);
	
	if(count($results)>0){
			$sql="update xAP_Telephone_Line set
			LineCountry='".dbesc($msg["body"]["line"]["country"])."',
			LineStatus='".dbesc($msg["body"]["line"]["linestate"])."',
			LineNetwork='".dbesc($msg["body"]["line"]["network"])."',
			LineDialler='".dbesc($msg["body"]["line"]["dialler"])."',
			LineDateTime='".dbesc(date("Y-m-d H:i:s"))."'
			where LineID=".$results[0]["LineID"];
			dbexec($sql);
			xAPLog("xAP","Line Updated ".$msg["header"]["source"],"");
	}
	else
	{
			$sql="insert into xAP_Telephone_Line 
			(LogID,LinexAPSource,LineCountry,LineStatus,LineNetwork,LineDialler,LineDateTime) VALUES
			(Null,
			'".strtolower(dbesc($msg["header"]["source"]))."',
			'".dbesc($msg["body"]["line"]["country"])."',
			'".dbesc($msg["body"]["line"]["linestate"])."',
			'".dbesc($msg["body"]["line"]["network"])."',
			'".dbesc($msg["body"]["line"]["dialler"])."',
			'".dbesc(date("Y-m-d H:i:s"))."')";

			dbexec($sql);
			xAPLog("xAP","Line Created ".$msg["header"]["source"],"");
	}
	
}


function xAPProcessCidIncoming($msg){


	$Value=$msg["body"]["cid.incoming"];

	$call=array(
			"direction"=>"Incoming",
			"source"=>strtolower($msg["header"]["source"]),
			"callnumber"=>$Value["phone"],
			"rawnumber"=>preg_replace("/[^0-9]/","",$Value["phone"]),
			"rnnumber"=>$Value["rnnumber"],
			"calltype"=>$Value["type"],
			"calldate"=>xAPTimeToDBTime($Value["datetime"]),
			"callname"=>$Value["name"],
			"network"=>$Value["network"],
			"callstart"=>date("Y-m-d H:i:s"),	
			"callend"=>date("Y-m-d H:i:s"),	
			"duration"=>0,
			"rings"=>0,
			"answered"=>"",
			"company"=>"",
			"stdmapref"=>"",
			"lookupsource"=>"",
			"lookup"=>"",
			"lookupref"=>"",
			"answered"=>"",
			"status"=>100);
	$call=xAPProcessTelephoneLookupNumber($call);	
	xAPProcessTelephoneLogCall($call);
	
}

function xAPProcessCidMeteor($msg){

	if (isset($msg["body"]["incoming.callwithcid"])){
		$Value=$msg["body"]["incoming.callwithcid"];
		$call=array(
			"direction"=>"Incoming",
			"source"=>strtolower($msg["header"]["source"]),
			"callnumber"=>$Value["phone"],
			"rawnumber"=>preg_replace("/[^0-9]/","",$Value["phone"]),
			"rnnumber"=>$Value["rnnumber"],
			"calltype"=>$Value["type"],
			"calldate"=>xAPTimeToDBTime($Value["datetime"]),
			"callname"=>$Value["name"],
			"callstart"=>date("Y-m-d H:i:s"),
			"callend"=>"",
			"network"=>$Value["network"],
			"duration"=>0,
			"rings"=>0,
			"answered"=>"",
			"company"=>"",
			"stdmapref"=>"",
			"lookupsource"=>"",
			"lookup"=>"",
			"lookupref"=>"",
			"answered"=>"",
			"dialler"=>$Value["dialler"],
			"status"=>10);
		$call=xAPProcessTelephoneLookupNumber($call);	
		xAPProcessTelephoneLogCall($call);
	}
	if (isset($msg["body"]["incoming.callcomplete"])){
		$Value=$msg["body"]["incoming.callcomplete"];
		$call=array(
			"direction"=>"Incoming",
			"source"=>strtolower($msg["header"]["source"]),
			"duration"=>$Value["duration"],
			"callend"=>date("Y-m-d H:i:s"),			
			"rings"=>$Value["rings"],
			"answered"=>"Yes",
			"status"=>100);
		xAPProcessTelephoneLogCall($call);
	}
	if (isset($msg["body"]["incoming.callnotanswered"])){
		$Value=$msg["body"]["incoming.callnotanswered"];
		$call=array(
			"direction"=>"Incoming",
			"source"=>strtolower($msg["header"]["source"]),
			"duration"=>$Value["duration"],
			"rings"=>$Value["rings"],
			"callend"=>date("Y-m-d H:i:s"),	
			"answered"=>"No",
			"status"=>100);
		xAPProcessTelephoneLogCall($call);
	}
	if (isset($msg["body"]["incoming.callanswered"])){
		$Value=$msg["body"]["incoming.callanswered"];
			// call events for pickup
		$call=array(
			"answered"=>"Yes",
			"status"=>50);
		$call["event"][]=array("event"=>"Incoming call picked up","desc"=>"When the handset of a phone s picked up on an incoming call.");
		xAPProcessTelephoneLogCall($call);
	}
	if (isset($msg["body"]["outgoing.callinitiated"])){
		$Value=$msg["body"]["outgoing.callinitiated"];
		$call=array(
			"direction"=>"Outgoing",
			"source"=>strtolower($msg["header"]["source"]),
			"callnumber"=>$Value["number"],
			"rawnumber"=>preg_replace("/[^0-9]/","",$Value["number"]),
			"rnnumber"=>"",
			"calltype"=>"",
			"calldate"=>xAPTimeToDBTime($Value["datetime"]),
			"callname"=>$Value["name"],
			"callstart"=>date("Y-m-d H:i:s"),
			"callend"=>"",
			"network"=>$Value["network"],
			"duration"=>0,
			"rings"=>0,
			"answered"=>"",
			"company"=>"",
			"stdmapref"=>"",
			"lookupsource"=>"",
			"lookup"=>"",
			"lookupref"=>"",
			"answered"=>"",
			"dialler"=>$Value["dialler"],
			"status"=>10);
		$call=xAPProcessTelephoneLookupNumber($call);	
		xAPProcessTelephoneLogCall($call);
	}
	if (isset($msg["body"]["outgoing.connected"])){
		$Value=$msg["body"]["outgoing.connected"];
		$call=array(
			"direction"=>"Outgoing",
			"source"=>strtolower($msg["header"]["source"]),
			"callnumber"=>$Value["dialled"].$Value["dialed"],
			"rawnumber"=>preg_replace("/[^0-9]/","",$Value["dialled"].$Value["dialed"]),
			"status"=>40);
		$call=xAPProcessTelephoneLookupNumber($call);	
		xAPProcessTelephoneLogCall($call);
	}
	if (isset($msg["body"]["outgoing.digitpressed"])){
		$Value=$msg["body"]["outgoing.digitpressed"];
		$call=array(
			"direction"=>"Outgoing",
			"source"=>strtolower($msg["header"]["source"]),
			"callnumber"=>$Value["dialled"].$Value["dialed"],
			"rawnumber"=>preg_replace("/[^0-9]/","",$Value["dialled"].$Value["dialed"]),
			"status"=>20);
		$call=xAPProcessTelephoneLookupNumber($call);	
		xAPProcessTelephoneLogCall($call);
	}
	if (isset($msg["body"]["outgoing.callanswered"])){
		$Value=$msg["body"]["outgoing.callanswered"];
		$call=array(
			"direction"=>"Outgoing",
			"source"=>strtolower($msg["header"]["source"]),
			"answered"=>"Yes",
			"status"=>30);	
		xAPProcessTelephoneLogCall($call);
	}
	if (isset($msg["body"]["outgoing.callcancelled"])){
		$Value=$msg["body"]["outgoing.callcancelled"];
		$call=array(
			"direction"=>"Outgoing",
			"source"=>strtolower($msg["header"]["source"]),
			"answered"=>"No",
			"status"=>30);	
		xAPProcessTelephoneLogCall($call);
	}
	if (isset($msg["body"]["outgoing.callcomplete"])){
		$Value=$msg["body"]["outgoing.callcomplete"];
		$call=array(
			"direction"=>"Outgoing",
			"source"=>strtolower($msg["header"]["source"]),
			"callnumber"=>$Value["dialled"].$Value["dialed"].$Value["phone"],
			"rawnumber"=>preg_replace("/[^0-9]/","",$Value["dialled"].$Value["dialed"].$Value["phone"]),
			"duration"=>$Value["duration"],
			"callend"=>date("Y-m-d H:i:s"),	
			"answered"=>"Yes",
			"status"=>100);	
		xAPProcessTelephoneLogCall($call);
	}
	if (isset($msg["body"]["outgoing.callterminated"])){
		$Value=$msg["body"]["outgoing.callterminated"];
		$call=array(
			"direction"=>"Outgoing",
			"source"=>strtolower($msg["header"]["source"]),
			"callnumber"=>$Value["dialled"].$Value["dialed"].$Value["phone"],
			"rawnumber"=>preg_replace("/[^0-9]/","",$Value["dialled"].$Value["dialed"].$Value["phone"]),
			"duration"=>$Value["duration"],
			"callend"=>date("Y-m-d H:i:s"),	
			"answered"=>"Yes",
			"status"=>100);	
		xAPProcessTelephoneLogCall($call);
	}
}









function xAPProcessTelephoneLookupNumber($call){

	// lookup the phone number and add name, location
	if($call["lookup"]!="Found" && $call["rawnumber"]!=""){   // Only lookup if note found
		$sql="select * from xAP_Contacts where 
		StripHomePhone='".dbesc($call["rawnumber"])."' or 
		StripHomePhone2='".dbesc($call["rawnumber"])."' or 
		StripBusinessPhone='".dbesc($call["rawnumber"])."' or 
		StripBusinessPhone2='".dbesc($call["rawnumber"])."' or 
		StripMobilePhone='".dbesc($call["rawnumber"])."'
		";
		$results=dbquery($sql);
		if (count($results)>0){

			$row=$results[0];
			if($row["StripHomePhone"]==$call["rawnumber"] || $row["StripHomePhone2"]==$call["rawnumber"]){
				$call["callname"]=$row["DisplayName"];
				if($call["callname"]==""){
					$call["callname"]=trim($row["FirstName"]." ".$row["LastName"]." ".$row["Company"]);
				}
				$call["stdmapref"]="P".$row["HomePostalCode"];
				if(strlen($row["HomeMapRef"]>3)){$call["stdmapref"]=$row["HomeMapRef"];}
			}
			elseif($row["StripBusinessPhone"]==$call["rawnumber"] || $row["StripBusinessPhone2"]==$call["rawnumber"]){
				$call["callname"]=$row["DisplayName"];
				if($call["callname"]==""){
					$call["callname"]=trim($row["FirstName"]." ".$row["LastName"]." ".$row["Company"]);
				}
				$call["company"]=$row["Company"];
				$call["stdmapref"]="P".$row["BusinessPostalCode"];
				if(strlen($row["HomeMapRef"]>3)){$call["stdmapref"]=$row["BusinessMapRef"];}
			}
			elseif($row["StripMobilePhone"]==$call["rawnumber"]){
				$call["callname"]=$row["DisplayName"];
				if($call["callname"]==""){
					$call["callname"]=trim($row["FirstName"]." ".$row["LastName"]." ".$row["Company"]);
				}
				$call["company"]=$row["Company"];
				$call["stdmapref"]="";
			}
		
			
			$call["lookupsource"]="Local_DB";
			$call["lookup"]="Found";
			$call["lookupref"]=$row["ConatctID"];
			if($call["callname"]!=""){
				xAPLog("xAP","Telephone Call Found Contact ",$call["callname"]);
				$call["event"][]=array("event"=>$call["direction"]." Contact Found","desc"=>"Happens on a call when the number is matched with the contacts database");
			}
		}
	}
	return $call;
}
function xAPProcessTelephoneLogCall($call){

	//Cleanups
	if(strtolower($call["callname"])=="international"){$call["callname"]="International";}
	if(strtolower($call["callname"])=="unavailable"){$call["callname"]="Unavailable";}
	if(strtolower($call["callname"])=="payphone"){$call["callname"]="Payphone";}
	if(strtolower($call["callname"])=="withheld"){$call["callname"]="Withheld";}

	// first update the line with information.
	$sql="select LineID from xAP_Telephone_Line where LinexAPSource='".strtolower(dbesc($call["source"]))."'";
	$results=dbquery($sql);
	if(count($results)==0){  // intheory the line should exist
			$sql="insert into xAP_Telephone_Line 
			(LineID,LinexAPSource,LineDateTime) VALUES
			(Null,
			'".strtolower(dbesc($call["source"]))."',
			'".dbesc(date("Y-m-d H:i:s"))."')";
			dbexec($sql);
		$sql="select LineID from xAP_Telephone_Line where LinexAPSource='".strtolower(dbesc($call["source"]))."'";
		$results=dbquery($sql);
	}
	$id=$results[0]["LineID"];
	$call["LineID"]=$id;
	//update the line table  but only the bits that are set
	$sql="update xAP_Telephone_Line Set ";
	if(isset($call["direction"])){$sql.= "Direction='".dbesc($call["direction"])."', ";}
	if(isset($call["callnumber"])){$sql.= "CallNumber='".dbesc($call["callnumber"])."', ";}
	if(isset($call["rawnumber"])){$sql.= "StrippedNumber='".dbesc($call["rawnumber"])."', ";}
	if(isset($call["rnnumber"])){$sql.= "RNNumber='".dbesc($call["rnnumber"])."', ";}
	if(isset($call["calltype"])){$sql.= "CallType='".dbesc($call["calltype"])."', ";}
	if(isset($call["calldate"])){$sql.= "CallDate='".dbesc($call["calldate"])."', ";}
	if(isset($call["callstart"])){$sql.= "CallStart='".dbesc($call["callstart"])."', ";}
	if(isset($call["callend"])){$sql.= "CallEnd='".dbesc($call["callend"])."', ";}
	if(isset($call["callname"])){$sql.= "CallName='".dbesc($call["callname"])."', ";}
	if(isset($call["network"])){$sql.= "Network='".dbesc($call["network"])."', ";}
	if(isset($call["rings"])){$sql.= "Rings='".dbesc($call["rings"])."', ";}
	if(isset($call["answered"])){$sql.= "Answered='".dbesc($call["answered"])."', ";}
	if(isset($call["company"])){$sql.= "Company='".dbesc($call["company"])."', ";}
	if(isset($call["stdmapref"])){$sql.= "MapRef='".dbesc($call["stdmapref"])."', ";}
	if(isset($call["lookupsource"])){$sql.= "LookupSource='".dbesc($call["lookupsource"])."', ";}
	if(isset($call["lookup"])){$sql.= "Lookup='".dbesc($call["lookup"])."', ";}
	if(isset($call["lookupref"])){$sql.= "LookupRef='".dbesc($call["lookupref"])."', ";}
	if(isset($call["status"])){$sql.= "status='".dbesc($call["status"])."', ";}
	
	$sql.=" where LineID=".$id;
	$sql=str_replace("',  where","'  where",$sql); //  remove the last comma
	dbexec($sql);
	xAPLog("xAP","Telephone ".$call["callname"],$call["callnumber"]);
	

	
	if($call["status"]==100){
		//status indicates a complete call
		//get duration
		$sql="select strftime(\"%s\",CallStart) as CS,strftime(\"%s\",CallEnd) as CE from xAP_Telephone_Line where LineID=".$id;
		$row=dbquery($sql);
		$sql="update xAP_TelePhone_Line set Duration=".($row[0]["CE"]-$row[0]["CS"])." where LineID=".$id;
		dbexec($sql);
		xAPProcessTelephoneLogCallFinal($id);
		//trigger an event
		$call["event"][]=array("event"=>$call["direction"]." Call Complete","desc"=>"Occurs when the call is completed");
	}
	//events???
	if (isset($call["event"])){
		foreach($call["event"] as $value){
			EventRunBySource("Telephone",$value["event"],$call,$value["desc"]);
		}
	}
}
function xAPProcessTelephoneLogCallFinal($lineid){
	$sql="insert into xAP_Telephone_Log (Direction,CallNumber,StrippedNumber,RNNumber,CallType,CallDate,CallName,Network,Rings,Answered,Company,MapRef,LookupSource,Lookup,LookupRef,Duration)
 Select
 Direction,CallNumber,StrippedNumber,RNNumber,CallType,CallDate,CallName,Network,Rings,Answered,Company,MapRef,LookupSource,Lookup,LookupRef,Duration
 from xAP_Telephone_Line where LineID=".$lineid." and (StrippedNumber<>'' or Callname<>'')";
	dbexec($sql);
	xAPLog("xAP","Telephone Call Logged",$lineid);
}


function xAPProcessTelephoneDatabase(){
	dbexec("CREATE TABLE IF NOT EXISTS xAP_Telephone_Line (LineID INTEGER  AUTO_INCREMENT PRIMARY KEY)");
	dbexec("ALTER TABLE xAP_Telephone_Line add LinexAPSource VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add LineCountry VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add LineStatus VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add LineNetwork VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add LineDialler VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add LineDateTime datetime");
	dbexec("ALTER TABLE xAP_Telephone_Line add CallName VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add CallNumber VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Telephone_Line add StrippedNumber VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Telephone_Line add RNNumber VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Telephone_Line add CallType VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Telephone_Line add Company VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add Lookup VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Telephone_Line add Pic VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add Location VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add STD VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add CallDate datetime");
	dbexec("ALTER TABLE xAP_Telephone_Line add Direction VARCHAR(10)");
	dbexec("ALTER TABLE xAP_Telephone_Line add Source VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add LookupSource VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add Answered VARCHAR(5)");
	dbexec("ALTER TABLE xAP_Telephone_Line add Duration VARCHAR(15)");
	dbexec("ALTER TABLE xAP_Telephone_Line add LookupRef VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add Rings VARCHAR(10)");
	dbexec("ALTER TABLE xAP_Telephone_Line add MapRef VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add NationalSTD VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add RawNumber VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add Network VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Line add Status INTEGER");
	dbexec("ALTER TABLE xAP_Telephone_Line add CallStart datetime");
	dbexec("ALTER TABLE xAP_Telephone_Line add CallEnd datetime");
	
	dbexec("CREATE TABLE IF NOT EXISTS xAP_Telephone_Log (LogID INTEGER  AUTO_INCREMENT PRIMARY KEY)");
	dbexec("ALTER TABLE xAP_Telephone_Log add CallName VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Log add CallNumber VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Telephone_Log add StrippedNumber VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Telephone_Log add RNNumber VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Telephone_Log add CallType VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Telephone_Log add Company VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Log add Lookup VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Telephone_Log add Pic VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Log add Location VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Log add STD VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Log add CallDate datetime");
	dbexec("ALTER TABLE xAP_Telephone_Log add Direction VARCHAR(10)");
	dbexec("ALTER TABLE xAP_Telephone_Log add Source VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Log add LookupSource VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Log add Answered VARCHAR(5)");
	dbexec("ALTER TABLE xAP_Telephone_Log add Duration VARCHAR(15)");
	dbexec("ALTER TABLE xAP_Telephone_Log add LookupRef VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Log add Rings VARCHAR(10)");
	dbexec("ALTER TABLE xAP_Telephone_Log add MapRef VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Log add NationalSTD VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Log add RawNumber VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Log add Network VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Telephone_Log add Status INTEGER");	

	
	dbexec("CREATE TABLE IF NOT EXISTS xAP_Contacts (ContactID INTEGER  AUTO_INCREMENT PRIMARY KEY)");
	dbexec("ALTER TABLE xAP_Contacts add Title VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add FirstName VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add MiddleName VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add LastName VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Suffix VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Company VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Department VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add JobTitle VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add BusinessStreet VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add BusinessStreet2 VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add BusinessStreet3 VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add BusinessCity VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add BusinessState VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add BusinessPostalCode VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add BusinessCountry VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add HomeStreet VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add HomeStreet2 VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add HomeStreet3 VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add HomeCity VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add HomeState VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add HomePostalCode VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add HomeCountry VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add OtherStreet VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add OtherStreet2 VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add OtherStreet3 VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add OtherCity VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add OtherState VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add OtherPostalCode VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add OtherCountry VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add AssistantsPhone VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add BusinessFax VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add BusinessPhone VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add BusinessPhone2 VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Callback VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add CarPhone VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add CompanyMainPhone VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add HomeFax VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add HomePhone VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add HomePhone2 VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add ISDN VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add MobilePhone VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add OtherFax VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add OtherPhone VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Pager VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add PrimaryPhone VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add RadioPhone VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Telex VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Account VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Anniversary VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add AssistantsName VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add BillingInformation VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Birthday VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add BusinessAddressPOBox VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Categories VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Children VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add DirectoryServer VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add EmailAddress VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add EmailType VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add EmailDisplayName VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Email2Address VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Email2Type VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Email2DisplayName VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Email3Address VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Email3Type VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Email3DisplayName VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Gender VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add GovernmentIDNumber VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Hobby VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add HomeAddressPOBox VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Initials VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add InternetFreeBusy VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Keywords VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Language VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Location VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add ManagersName VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Mileage VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Notes VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add OfficeLocation VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add OrganizationalIDNumber VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add OtherAddressPOBox VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Priority VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Private VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Profession VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add ReferredBy VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Sensitivity VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add Spouse VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add User1 VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add User2 VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add User3 VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add User4 VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add WebPage VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add DisplayName VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add NickName VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Contacts add dbSource VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Contacts add dbSourceRef VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Contacts add dbSourceedit VARCHAR(10)");
	dbexec("ALTER TABLE xAP_Contacts add dbSourceeditwaiting VARCHAR(10)");
	dbexec("ALTER TABLE xAP_Contacts add dbSync VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Contacts add RecPriority INTEGER");
	dbexec("ALTER TABLE xAP_Contacts add StripHomePhone VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Contacts add StripHomePhone2 VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Contacts add StripBusinessPhone VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Contacts add StripBusinessPhone2 VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Contacts add StripMobilePhone VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Contacts add StripPrimaryPhone VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Contacts add StripCarPhone VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Contacts add IM1 VARCHAR(100)");
	dbexec("ALTER TABLE xAP_Contacts add IM2 VARCHAR(100)");
	dbexec("ALTER TABLE xAP_Contacts add IM3 VARCHAR(100)");
	dbexec("ALTER TABLE xAP_Contacts add IM4 VARCHAR(100)");
	dbexec("ALTER TABLE xAP_Contacts add IM5 VARCHAR(100)");
	dbexec("ALTER TABLE xAP_Contacts add NetworkBusinessPhone VARCHAR(20)");
	dbexec("ALTER TABLE xAP_Contacts add NetworkBusinessPhone2 VARCHAR(20)");
	dbexec("ALTER TABLE xAP_Contacts add HomeMapRef VARCHAR(20)");
	dbexec("ALTER TABLE xAP_Contacts add BusinessMapRef VARCHAR(20)");
	dbexec("ALTER TABLE xAP_Contacts add OtherMapRef VARCHAR(20)");
	dbexec("ALTER TABLE xAP_Contacts add NetworkHomePhone VARCHAR(20)");
	dbexec("ALTER TABLE xAP_Contacts add NetworkHomePhone2 VARCHAR(20)");
	dbexec("ALTER TABLE xAP_Contacts add NetworkPrimaryPhone VARCHAR(20)");
	dbexec("ALTER TABLE xAP_Contacts add NetworkCarPhone VARCHAR(20)");
	dbexec("ALTER TABLE xAP_Contacts add NetworkMobilePhone VARCHAR(100)");
	dbexec("update xAP_Contacts set RecPriority='9' where RecPriority='' or isnull(RecPriority)=True");

	dbexec("CREATE TABLE IF NOT EXISTS xAP_Messenger (MessengerID INTEGER  AUTO_INCREMENT PRIMARY KEY)");
	dbexec("ALTER TABLE xAP_Messenger add Network VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Messenger add xAPSource VARCHAR(100)");
	dbexec("ALTER TABLE xAP_Messenger add Username VARCHAR(100)");
	dbexec("ALTER TABLE xAP_Messenger add FullName VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Messenger add State VARCHAR(20)");
	dbexec("ALTER TABLE xAP_Messenger add DateStamp DATETIME");
}
/*
function xAPProcessTelephoneLogCall($call){
		$sql="insert into xAP_Telephone_Log 
		(LogID,CallName,CallNumber,StrippedNumber,RNNumber,CallType,Company,Lookup,Pic,
		Location,STD,CallDate,Direction,Source,LookupSource,Answered,Duration,Duration,
		LookupRef,Rings,MapRef,NationalSTD,RawNumber,Network,Status) 
		VALUES (
		Null,
		'".dbesc(strtolower($msg["header"]["source"]))."',
		$active,
		'".dbesc(($Value["name"]))."',
		'".dbesc(($Value["desc"]))."',
		'".date("Y-m-d H:i:s")."',
		'".date("Y-m-d H:i:s")."'
		)";
		dbexec($sql);
		xAPLog("xAP","Telephone ".$call["name"],$call["number"]);
}*/
?>